package com.songtai.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.songtai.dao.NodeDao;
import com.songtai.po.Node;
import com.songtai.pojo.NodeView;
import com.songtai.service.NodeService;

/**
 * @author zzy
 * @category 节点操作业务逻辑实现类
 */
public class NodeServiceImpl implements NodeService {
	private NodeDao nodeDao;

	@Override
	public void addNode(Node node) {
		nodeDao.addNode(node);
	}

	@Override
	public List<NodeView> getChildren(Long nodeid) {
		Set<Node> nodes = nodeDao.getChildrens(nodeid);
		if (nodes != null && !nodes.isEmpty()) {
			List<NodeView> list = new ArrayList<NodeView>();
			for (Node node : nodes) {
				NodeView view = new NodeView();
				view.setId(node.getId());
				view.setInsertDate(node.getInsertDate());
				view.setNodeName(node.getNodeName());
				view.setPosition(node.getPosition());
				view.setStatus(node.getStatus());
				list.add(view);
			}
			return list;
		}
		return null;
	}

	/**
	 * 获取兄弟节点
	 */
	@Override
	public List<NodeView> getAllBrothers(Long nodeid) {
		Set<Node> nodes = nodeDao.getBrothers(nodeid);
		System.out.println(nodes.size());
		if (nodes != null && !nodes.isEmpty()) {
			List<NodeView> list = new ArrayList<NodeView>();
			for (Node node : nodes) {
				NodeView view = new NodeView();
				view.setId(node.getId());
				view.setInsertDate(node.getInsertDate());
				view.setNodeName(node.getNodeName());
				view.setPosition(node.getPosition());
				view.setStatus(node.getStatus());
				list.add(view);
			}
			return list;
		}
		return null;
	}

	/**
	 * 通过id获取节点信息
	 * 
	 * @param nodeid
	 * @return
	 */
	@Override
	public Node getNodeById(Long nodeid) {
		return nodeDao.getNodeById(nodeid);
	}

	/**
	 * @return the nodeDao
	 */
	public NodeDao getNodeDao() {
		return nodeDao;
	}

	/**
	 * @param nodeDao
	 *            the nodeDao to set
	 */
	public void setNodeDao(NodeDao nodeDao) {
		this.nodeDao = nodeDao;
	}

}
